import { ObjectId } from "mongodb";
import { NextApiRequest, NextApiResponse } from "next";
import { VerifyFields } from "../../../src/api/utils";
import DB from "../mongodb";
import { AllowCORS, receiveRequest } from "../utils";

export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
    await AllowCORS(req, res);
    if (!receiveRequest(req, res, "POST")) return;
    const db = await DB;
    console.log("违规动态", req.body);
    if (!VerifyFields.has(req.body, ["_id", "allow"])) {
        return res.status(500).json("缺少关键字段");
    }

    const update = await db.collection("dynamic").updateOne(
        { _id: new ObjectId(req.body._id) },
        {
            $set: {
                allow: req.body.allow,
            },
        }
    );

    if (update.modifiedCount) {
        res.status(200).json({ data: "成功" });
        return;
    }
    res.json({ data: null, msg: "失败", code: -1 });
}
